<?php
namespace Admin\Model;
use Zend\Db\TableGateway\TableGateway;
use Zend\Debug\Debug;
use Zend\Db\Adapter\Adapter;
use Zend\Db\Sql\Select;
use Zend\Db\Sql\Sql;

class BillTable{
	protected $tableGateway;

	public function __construct(TableGateway $table){
		$this->tableGateway = $table;
	}

	public function fetchAll(){
		$result = $this->tableGateway->select();
		return $result;
	}

	public function getBill($id = ''){
		// lấy thông tin 1 hóa đơn
		$id = (int)$id;
		$rowset = $this->tableGateway->select(array('maHoaDon'=>$id));
		$result = $rowset->current();
		return $result;
	}

	public function deleteBill($id = ''){
		// xóa hóa đơn
		$id = (int)$id;
		$result = $this->tableGateway->delete(array('maHoaDon'=>$id));
		return $result;
	}

	public function editBill($arrParams = array()){
		// sửa hóa đơn

	}

	// tạo mới và lưu hóa đơn
	public function saveBill($arrParams = array()){
		// lưu hóa đơn
		if($arrParams['submit'])
			unset($arrParams['submit']);
		$arrParams['maHoaDon'] = time();
		$result = $this->tableGateway->insert($arrParams);
		return $result;
	}

	// lấy doanh thu
	public function getIncome(){
		$adapter = $this->getAdapter();

		$today = date("Y-m-d");
		$past = date("Y-m-d",strtotime("-3 Months"));
		$past = substr($past, 0, strlen($past)-2);
		$past .= "01";
		$sql = new Sql($adapter);
		$select = $sql->select('hoadon');
		$select->columns(array('soLuong','gia','ngay'));
		$predicate = new \Zend\Db\Sql\Where();
		$select->where($predicate->greaterThan('ngay',$past));
		$select->order('ngay ASC');
		$selectString = $sql->getSqlStringForSqlObject($select);
		$data = $adapter->query($selectString, $adapter::QUERY_MODE_EXECUTE);
		$data = $data->toArray();
		return $data;
	}


	public function getStatistic($from = '',$to = ''){
		$adapter = $this->getAdapter;
		$sql = new Sql($adapter);
		$select = $sql->select('hoadon');

	}

	public function getAdapter(){
		$adapter = new Adapter(array(
		    'driver' => 'Pdo_Mysql',
		    'database' => 'giayvinhxinh',
		    'username' => 'root',
		    'password' => '',
		    'driver_options'=>  array(
				1002=>'SET NAMES utf8'
			),
		 ));
		return $adapter;
	}
}